const axios = require('axios');

class livingDevice {
    token = ""
    fileList = []
    device = {}
    room_number = ""
    constructor(device, room_number) {
        this.device = device
        this.room_number = room_number
        return (async () => {
            let loginMessage = await this.login();
            return this;
        })();
    }
    async login() {
        // 登录
        let requestData = { userName: "admin", password: "admin", md5Flag: 0 };
        let url = "http://" + this.device.deviceIp + "/sdk/LoginSystem";
        try {
            let { data } = await axios.post(url, requestData);
            this.token = data.data.token;
        } catch (err) {
            return "fail";
        }
    }
    // 录制
    async startRecord() {
        // 开始录像
        let url = "http://" + this.device.deviceIp + "/sdk/StartRecord?token=" + this.token;
        let { data } = await axios.get(url)
        return data;
    }
    async endRecord() {
        // 停止直播
        const url = "http://" + this.device.deviceIp + "/sdk/StopRecord?token=" + this.token;
        const { data } = await axios.get(url)
        return data
    }
    // 推流
    async StartRtmp() {
        // 开始推流
        let stream = "rtmp://" + this.device.deviceIp + ":1935/live/" + this.room_number
        let url = "http://" + this.device.deviceIp + "/sdk/StartRtmp?channel=0&token=" + this.token + "&stream=" + stream;
        let { data } = await axios.get(url)
    }
    async StopRtmp() {
        // 停止直播
        const url = "http://" + this.device.deviceIp + "/sdk/StopRtmp?channel=0&token=" + this.token;
        const { data } = await axios.get(url)
        return data
    }
    // publishMode(0-live、1-record、2-append)
    async SetRtmpConfig(publishMode) {
        // 设置推流状态
        const url = "http://" + this.device.deviceIp + "/sdk/SetRtmpConfig";
        let requestData = { token: this.token, publishMode: publishMode }
        const { data } = await axios.post(url, requestData)
        return data
    }
    // async Set
    // 文件
    async getFileList() {
        //  获取所有文件名为rec的文件
        let url = "http://" + this.device.deviceIp + "/file/fileList";
        let payload = {
            token: this.token,
            fileName: "PGM.mp4",
            type: 1,
        };
        let queryString = "";
        Object.keys(payload).forEach((key) => {
            queryString += key + "=" + payload[key] + "&";
        });
        queryString = "?" + queryString.slice(0, -1);
        url = url + queryString;
        const data = await axios.get(url);
        this.fileList = data.data.data.fileList;
    }
    async getDownLoadUrl() {
        // 获取文件
        await this.getFileList();
        if (this.fileList.length == 0) {
            return ""
        }
        // 获取下载连接
        let downLoadUrl =
            "http://" +
            this.device.deviceIp + "/file/download/" +
            this.fileList[0].filePath +
            "/" +
            this.fileList[0].fileName +
            "?token=" +
            this.token;
        return downLoadUrl;
    }
    async deleteAllFiles() {
        // 刪除
        await this.getFileList()
        if (!this.fileList.length) {
            return;
        }
        let url = "http://" + this.device.deviceIp + "/file/delete//disk-0/rec_" + this.device.deviceID + "?token=" + this.token;
        let { data } = await axios.get(url)
    }
    // status 0关闭1开启
    async RtmpAddRecord(status) {
        let url = "http://"
            + this.device.deviceIp
            + "/sdk/OperateOnekeyCmd?status="
            + status
            + "&token="
            + this.token
        let data = await axios.get(url);
        return data;
    }
    async getDeviceMessage() {
        let url = "http://" + this.device.deviceIp + "/sdk/GetSysInfoConfig?token=" + this.token;
        let data = axios.get(url);
        return data;
    }
    // 检验设备中是否已经存在录播文件
    async checkFile() {
        let url = "http://" + this.device.deviceIp + "/file/fileList";
        let payload = {
            token: this.token,
            fileName: "PGM.mp4",
            type: 1,
        };
        let queryString = ""
        Object.keys(payload).forEach((key) => {
            queryString += key + "=" + payload[key] + "&";
        });
        queryString = "?" + queryString.slice(0, -1);
        url = url + queryString;
        const data = await axios.get(url);
        return data;
    }
    // 暂停录制
    async pauseRecord() {
        const url = "http://" + this.device.deviceIp + "/sdk/PauseRecord?token=" + this.token;
        const { data } = await axios.get(url)
        return data
    }
}

module.exports = livingDevice;